; const headJs = $(function () {
    //如果登录显示登录名
    var user1 = JSON.parse(($.cookie("userinfo")) || "{}");
    $.ajax({
        url: "../../api/login",
        data: {
            uname: user1.uname,
            upwd: user1.upwd
        },
        type: "post",
        dataType: "json"
    }).then((data) => {
        if (data.status == -1) {
            $(".nologin").show();
            $(".loger").hide();
        } else {
                $(".nologin").hide();
                $(".loger").show();
                $(".loginame").text(user1.uname);
        }
    })
    $(".exit").on("click",function () {
        $.cookie("userinfo","",-1);
        $(".nologin").show();
        $(".loger").hide();
    })


    //头部购物车移入出现
    $(".top-cart").on("mouseenter", function () {
        $(".nav-cart-detail").show();
    })
    //头部购物车离开消失
    $(".nav-cart-detail").on("mouseleave", function () {
        $(".nav-cart-detail").hide();
    })
    //导航分类出现
    $(".J_mBnCat").on("mouseenter", function () {
        $(".m-bn-con").show();
    })
    // 导航分类隐藏
    $(".m-bn-con").on("mouseleave", function () {
        $(".m-bn-con").hide();
    })
    // 导航栏实现分页效果
    $(".J_bnCate").find("li").on("mouseenter", function () {
        var index = $(this).index();
        $(".bn-con").find("ul").eq(index).show().siblings("ul").hide();
        $(this).addClass("current").siblings("li").removeClass("current");
    })
    // 搜索引擎
    //移入input出现搜索内容ul
    $(".J_btSpritPanel").on("mouseenter", function () {
        $("ul.search").show();
        $("ul.search").html("");
    })
    $(".J_btSpritPanel").on("mouseleave", function () {
        $(".ul.search").hide();

    })
    // 使用jsnp链接端口
    $(".J_bnSearchInput").on("input", function () {
        jsonpTool.getJSONP({
            url: "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",
            data: {
                wd: this.value
            },
            jsonpCallback: "cb"
        }).then(function (data) {
            $("ul.search").html("");
            var strHtml = ``;
            data.s.forEach(el => {
                strHtml += `<li>${el}</li>`;
            });
            $("ul.search").html(strHtml);
        })
    })
    $("ul.search").on("click", function (e) {
        console.log()
        var target = e.target;
        if (target.nodeName == "LI") {
            $(".J_bnSearchInput").val(target.innerText);
            $("ul.search").hide();
        }
    })
    //移入的li高亮效果
    $("ul.search").on("mouseover", "li", function (e) {
        var index = $(this).index();
        $("ul.search").find("li").eq(index).addClass("current").siblings("li").removeClass("current");
    })
    //固定导航栏
    var beibei_warnH = $(".beibei_warn").height();//第一个高度
    var top_navH = $(".top_nav").height();//第二个高度
    $(document).on("scroll", function () {
        var dH = beibei_warnH + top_navH;
        var mH = $(this).scrollTop();
        if (mH >= dH) {
            $(".sub-nav").addClass("fixed");
            $("section").css({
                "marginTop": $(".sub-nav").height()
            })
        } else {
            $(".sub-nav").removeClass("fixed");
            $("section").css({
                "marginTop": 0
            })
        };

    });
    async function show() {
        var result = await $.get("../../api/getcarts", {
            uid: JSON.parse($.cookie("userinfo") || "[]").uId,
        })
        var shopcartsTotalPrice = 0;
        num = result.length;
        if (result.length == 0) {
            $(".empty-cart").show();
            $(".carlis").hide();
        } else {
            $(".empty-cart").hide();
            $(".carlis").show();
            console.log(result)
            result.forEach(el => {
                var sidecart = `
           <li wg-view="item"
           style="display: list-item; padding: 12px; list-style: none; position: relative; overflow: hidden; height: 60px; border-bottom: none; class="carlis" ">
           <a href="./goodsdetails.html?pid=${el.pid}" wg-view="imglink" op-value="itemLink" op-attr="href"><img
                   wg-view="img" op-value="imgUrl" op-attr="src"
                   src="${el.pImg}"
                   style="border: 2px solid rgb(204, 204, 204); background: rgb(255, 255, 255); padding: 2px; width: 50px; height: 50px; position: relative; z-index: 4;"></a><a
               href="./goodsdetails.html?pid=${el.pid}" wg-view="name"
               op-value="title,itemLink" op-attr="*,href"
               style="position: absolute; left: 0px; top: 15px; padding: 0px 10px 0px 80px; line-height: 1.2em; height: 30px; overflow: hidden; font-size: 13px; margin: 0px; width: 50%; z-index: 3;">${el.pName}</a>
           <p wg-view="sku" op-value="sku_props"
               style="position: absolute; color: rgb(170, 170, 170); left: 0px; top: 52px; padding: 0px 10px 0px 80px; line-height: 1.2em; height: 14px; overflow: hidden; font-size: 12px; margin: 0px; width: 50%;">
               颜色:${el.pSize}</p>
           <p wg-view="price" op-value="price"
               style="position: absolute; color: rgb(255, 84, 130); right: 15px; top: 25px; overflow: hidden; font-size: 16px; padding-left: 80px; margin: 0px; width: 30%; line-height: 1em; text-align: right;">
            ¥${el.pPrice}<br><span wg-view="num"
                   style="display: inline; color: rgb(136, 136, 136); font-size: 13px;">
                   x${el.pNum}</span></p>
       </li>
           `
                $(".cartInfor").append(sidecart);

                //加减
                 //取出购物车对象,循环,计算总价
            let shopcarts = JSON.parse(window.localStorage.getItem("shopcars")||"[]");
           
            shopcarts.map((item) => {
                shopcartsTotalPrice += item.pNum * parseInt(item.pPrice)
            })
            })
        
        }
        console.log(shopcartsTotalPrice)
        $(".headcarT").text(shopcartsTotalPrice)
        $(".goodnum").text(num)
        console.log(num)
    }
    show()
    function sideClick() {
        let user2 = JSON.parse(($.cookie("userinfo")) || "{}");
        $(".main_logo").on("click",".top-cart,.tocars",function () {
            console.log(1111)
            $.ajax({
                url: "../../api/login",
                data: {
                    uname: user2.uname,
                    upwd: user2.upwd
                },
                type: "post",
                dataType: "json"
            }).done((data=>{
            if (data.status == -1) {
                console.log(11111)
                layer.confirm("你还没有登录",{
                    btn:["去","不去"]
                },
                (index)=>{
                    layer.close(index);
                    window.location="./login.html"
                },
                (index)=>{
                    layer.close(index);
                    return
                }
                )
            }else{
                window.location="./shopcar.html"
            }
            }))
        })
    }
    sideClick()
})